package cn.hairui.blog.controller;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.*;
import cn.hairui.blog.service.*;
import cn.hairui.blog.util.CommUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * <p>
 * <h2>主页控制器</h2>
 * <ol>请添加详细描述
 * </ol>
 * </p>
 *
 * @author lihairui
 * @version 1.0
 * @Date 2019年6月7日 下午12:41:01
 * @since jdk 1.8
 */
@Controller
public class HomePageController {
    @Autowired
    private WebInfoService webInfoService;
    @Autowired
    private TabMenuService tabMenuService;
    @Autowired
    private ArticalService articalService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private BookService bookService;
    @Autowired
    private ToolService toolService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private LinkService linkService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String homepage(Model model) {
        //查询网站信息
        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);

        //tab页签标题信息
        List<TabMenu> tabMenuList = tabMenuService.listShow();
        model.addAttribute("tabMmenuList", tabMenuList);

        //这里首页tab页签中只显示指定条数内容
        List<List<Artical>> tabArticalList = new ArrayList<List<Artical>>();
        for (TabMenu nav : tabMenuList) {
            List<Artical> listArt = articalService.queryByNavid(nav.getId(), PubConstant.TAB_CONTENT_SHOW_MAX);
            tabArticalList.add(listArt);
        }
        model.addAttribute("tabArticalList", tabArticalList);

        //查询要展示的分类
        List<Category> categoryList = categoryService.findByIsShow(PubConstant.YES_NO_Y);
        model.addAttribute("categoryList", categoryList);

        //分类中的内容
        List<List<Artical>> articals = new ArrayList<List<Artical>>();
        for (Category artical : categoryList) {
            //指定首页分类块中每块显示指定条记录
            List<Artical> articalList = articalService.findByCategoryIdLimited(artical.getId(), PubConstant.MAX_SHOW_ARTICAL_IN_CATEGORIES);
            articals.add(articalList);
        }
        model.addAttribute("articals", articals);

        //读取指定数量专题
        List<Topic> topicList = topicService.findLimited(PubConstant.MAX_SHOW_ARTICAL_TOPICS);
        model.addAttribute("topicList", topicList);

        //读取指定数量开源书籍
        List<Book> booksList = bookService.findLimited(0, PubConstant.MAX_SHOW_TOOLS);
        model.addAttribute("bookListLeft", booksList);
        List<Book> booksList2 = bookService.findLimited(PubConstant.MAX_SHOW_TOOLS, PubConstant.MAX_SHOW_TOOLS);
        model.addAttribute("bookListRight", booksList2);

        //在线工具
        List<Tool> toolList = toolService.findLimited(PubConstant.MAX_SHOW_TOOLS);
        model.addAttribute("toolList", toolList);

        //热门文章
        List<Artical> hotArticalList = articalService.findHotLimited(PubConstant.TOP_LIKE_ARTICAL);
        Iterator iterator = hotArticalList.iterator();
        List<Map> hotarticals = new ArrayList();
        while (iterator.hasNext()) {
            Artical artical = (Artical) iterator.next();
            Map map = new HashMap();
            map.put("id", artical.getId());
            map.put("title", artical.getTitle());
            map.put("month", artical.getCreatedate().substring(5, 7));
            map.put("day", artical.getCreatedate().substring(8, 10));
            hotarticals.add(map);
        }
        model.addAttribute("hotarticals", hotarticals);


        int pageSize = 10;
        //获取置顶文章
        List<Artical> articalTop = articalService.findTop();
        List<Map> articalTopList = new ArrayList<Map>();
        Iterator iterator1 = articalTop.iterator();
        while (iterator1.hasNext()) {
            Map map = new HashMap();
            Artical artical = (Artical) iterator1.next();
            map.put("id", artical.getId());
            map.put("title", artical.getTitle());
            map.put("summary", artical.getSummary());
            map.put("category", artical.getCategoryid());
            map.put("createdate", artical.getCreatedate());
            map.put("author", userService.findByUsername(artical.getAuthor()).getNickname());
            map.put("avatar", userService.findByUsername(artical.getAuthor()).getProphoto());
            int caId = artical.getCategoryid();
            String categoryidName = categoryService.find(caId).getCategoryName();
            map.put("categoryId", caId);
            map.put("categoryName", categoryidName);
            String tags = artical.getTags();
            List tagsList = CommUtil.tagStrToList(tags);
            map.put("tags", tagsList);
            articalTopList.add(map);
        }
        model.addAttribute("articalTop", articalTopList);

        //首页展示文章
        PageHelper.startPage(1, pageSize);
        List<Artical> articalsAll = articalService.list();

        PageInfo<Artical> pageInfo = new PageInfo<Artical>(articalsAll);
        List<Map> articalList = new ArrayList<Map>();
        //将对象转换为map 并在map中添加新的属性
        Iterator articalIterator = articalsAll.iterator();
        while (articalIterator.hasNext()) {
            Map map = new HashMap();
            Artical artical = (Artical) articalIterator.next();
            map.put("id", artical.getId());
            map.put("title", artical.getTitle());
            map.put("summary", artical.getSummary());
            map.put("category", artical.getCategoryid());
            map.put("createdate", artical.getCreatedate());
            map.put("author", userService.findByUsername(artical.getAuthor()).getNickname());
            map.put("avatar", userService.findByUsername(artical.getAuthor()).getProphoto());
            int caId = artical.getCategoryid();
            String categoryidName = categoryService.find(caId).getCategoryName();
            map.put("categoryId", caId);
            map.put("categoryName", categoryidName);
            String tags = artical.getTags();
            List tagsList = CommUtil.tagStrToList(tags);
            map.put("tags", tagsList);
            articalList.add(map);
        }
        model.addAttribute("articalsList", articalList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", 1);
        model.addAttribute("pageSize", pageSize);


        //查询统计标签 最多显示指定条数
        List<Map> tags = articalService.findtagsLimited(PubConstant.MAX_SHOW_ARTICALTAGS);
        model.addAttribute("tags", tags);

        //查询审核通过留言 显示指定条数
        List<Map> comments = messageService.findLimited(PubConstant.STS_1, PubConstant.MAX_SHOW_COMMENTS);
        model.addAttribute("comments", comments);

        //友情链接
        List<Link> links = linkService.findLimited(PubConstant.STS_1, PubConstant.TWENTY);
        model.addAttribute("links", links);

        return "index";
    }

    @RequestMapping(value = "me")
    public String me(Model model) {
        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);
        return "me";
    }

    @RequestMapping(value = "profess")
    public String profess(Model model) {
        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);
        return "profess";
    }

    @RequestMapping(value = "mikutap")
    public String mikutap(Model model) {
        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);
        return "mikutap";
    }

}
